package site.wanjiahao.handler.server;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import site.wanjiahao.message.request.GroupChartRequestMessage;
import site.wanjiahao.message.response.ChartResponseMessage;
import site.wanjiahao.message.response.GroupChartResponseMessage;
import site.wanjiahao.session.Session;
import site.wanjiahao.session.SessionFactory;

import java.util.Set;

@ChannelHandler.Sharable
public class GroupChartRequestMessageHandler extends SimpleChannelInboundHandler<GroupChartRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupChartRequestMessage msg) throws Exception {
        // 获取当前组名称对应的通道对象
        Set<Channel> channels = SessionFactory.getGroupSession().listChannel(msg.getGName());
        String from = SessionFactory.getSession().getUsername(ctx.channel());
        // 发送数据到各自channel上
        for (Channel channel : channels) {
            channel.writeAndFlush(new GroupChartResponseMessage(msg.getGName(), from, msg.getContent()));
        }
    }
}
